#!/bin/sh
#
# helps bootstrapping, when checked out from CVS
# requires GNU autoconf and GNU automake
#
# this is not meant to be exported outside the source tree
#
# NOTE: Inspired by libtool's autogen script
#
# to be run from the toplevel directory of RTEMS'
# source tree

progname=`basename $0`
top_srcdir=`dirname $0`

LC_ALL=C
export LC_ALL

verbose=""
quiet="false"
mode="autoreconf"
force=0

usage()
{
  echo
  echo "usage: ${progname} [-c|-p|-h] [-q][-v]"
  echo
  echo "options:"
  echo "	-c .. clean, remove all aclocal/autoconf/automake generated files"
  echo "	-h .. display this message and exit"
  echo "	-H .. regenerate headers.am files"
  echo "	-q .. quiet, don't display directories"
  echo "	-v .. verbose, pass -v to autotools"
  echo
  exit 1
}

if test ! -f $top_srcdir/aclocal/version.m4; then
  echo "${progname}:"
  echo "	Installation problem: Can't find file aclocal/version.m4"
  exit 1
fi

while test $# -gt 0; do
case $1 in
-h|--he|--hel|--help)
  usage ;;
-q|--qu|--qui|--quie|--quiet)
  quiet="true"
  shift;;
-v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
  verbose="-v"
  shift;;
-c|--cl|--cle|--clea|--clean)
  mode="clean"
  shift;;
-f|--fo|--for|--forc|--force)
  force=`expr $force + 1`
  shift;;
-H|--headers)
  mode="headers"
  shift;;
-r|--re|--rec|--reco|--recon|--reconf)
  mode="autoreconf"
  shift;;
-g|--ge|--gen|--gene|--gener|--genera|--generat|--generate)
  mode="generate"
  shift;;
-*) echo "unknown option $1"
  usage ;;
*) echo "invalid parameter $1"
  usage ;;
esac
done

case $mode in
headers)
  if test "." != "$top_srcdir"; then
    echo "To generate the headers.am you must call the script via \"./$progname -H\""
    exit 1
  fi
  base="$PWD"
  tmp="$base/headers.am.new"
  for i in cpukit/include cpukit/score/cpu/*/include bsps/include bsps/*/include bsps/*/*/include ; do
    dir=""
    am_dir=""
    echo '## This file was generated by "./boostrap -H".' > "$tmp"
    case $i in
      cpukit/include)
        hdr="../"
        inc="include/"
        ;;
      cpukit/score/cpu/*/include)
        hdr="../"
        inc=`echo $i | cut -d'/' -f5-`
        inc="$inc/"
        ;;
      bsps/*/*/include)
        hdr="../"
        inc="../../../../../../$i/"
        ;;
      bsps/*/include)
        hdr="../"
        inc="../../../../../$i/"
        ;;
      bsps/include)
        hdr="../"
        inc="../../$i/"
        ;;
      *)
        hdr=""
        inc=""
        ;;
    esac
    cd $i
    for b in `find -type d | sort` ; do
      for j in `find $b -mindepth 1 -maxdepth 1 -name '*.h' -or -name '*.inc' | sed 's%^\.%%' | sed 's%^/%%' | sort` ; do
        d=`dirname $j`
        if test x$d != x$dir ; then
          dir=$d
          if test x$d != x. ; then
            am_dir=`echo $dir | sed 's%[/-]%_%g'`
            am_dir="_$am_dir"
            printf "\ninclude%sdir = \$(includedir)/$dir\n" "$am_dir" >> "$tmp"
          else
            am_dir=""
            echo "" >> "$tmp"
          fi
          echo "include${am_dir}_HEADERS =" >> "$tmp"
        fi
        echo "include${am_dir}_HEADERS += $inc$j" >> "$tmp"
        if test $j = bsp.h ; then
          echo "include_HEADERS += include/bspopts.h" >> "$tmp"
        fi
      done
    done
    cd "$base"
    diff -q "$tmp" "$i/${hdr}headers.am" || mv "$tmp" "$i/${hdr}headers.am"
  done
  rm -f "$tmp"
  ;;

generate)
  AUTOCONF=${AUTOCONF-autoconf}
  if test -z "$AUTOCONF"; then
    echo "You must have autoconf installed to run $program"
    exit 1
  fi

  AUTOHEADER=${AUTOHEADER-autoheader}
  if test -z "$AUTOHEADER"; then
    echo "You must have autoconf installed to run $program"
    exit 1
  fi

  AUTOMAKE=${AUTOMAKE-automake}
  if test -z "$AUTOMAKE"; then
    echo "You must have automake installed to run $program"
    exit 1
  fi

  ACLOCAL=${ACLOCAL-aclocal}
  if test -z "$ACLOCAL"; then
    echo "You must have automake installed to run $program"
    exit 1
  fi

  case $top_srcdir in
  /* ) aclocal_dir=$top_srcdir
    ;;
  *) aclocal_dir=`pwd`/$top_srcdir
    ;;
  esac

  confs=`find . \( -name 'configure.in' -o -name 'configure.ac' \) -print`
  for i in $confs; do
  dir=`dirname $i`
  configure=`basename $i`
  ( test "$quiet" = "true" || echo "$dir"
    cd $dir
    pat="s,\$(RTEMS_TOPdir),${aclocal_dir},g"
    aclocal_args=`grep '^[ ]*ACLOCAL_AMFLAGS' Makefile.am | \
      sed -e 's%.*ACLOCAL_AMFLAGS.*\=[ ]*%%g' -e $pat `
    test "$verbose" = "-v" && echo "${ACLOCAL} $aclocal_args"
    ${ACLOCAL} $aclocal_args
    test -n "`grep CONFIG_HEADER ${configure}`" && ${AUTOHEADER} \
      && test "$verbose" = "-v" && echo "${AUTOHEADER}"
    test -n "`grep RTEMS_BSP_CONFIGURE ${configure}`" && ${AUTOHEADER} \
      && test "$verbose" = "-v" && echo "${AUTOHEADER}"
    test -f Makefile.am && ${AUTOMAKE} -a -c $verbose
    ${AUTOCONF}
    test -f Makefile.am && test -n "`grep 'stamp-h\.in' Makefile.in`" \
      && echo timestamp > stamp-h.in
  )
  done
  ;;

autoreconf)
  AUTORECONF=${AUTORECONF-autoreconf}
  if test -z "$AUTORECONF"; then
    echo "You must have autoreconf installed to run $program"
    exit 1
  fi

  confs=`find . -name 'configure.ac' -print`
  for i in $confs; do
  dir=`dirname $i`
  configure=`basename $i`
  ( test "$quiet" = "true" || echo "$dir"
    cd $dir
    ${AUTORECONF} -i --no-recursive $verbose
    test -f Makefile.am && test -n "`grep 'stamp-h\.in' Makefile.in`" \
      && echo timestamp > stamp-h.in
  )
  done
  ;;

clean)
  test "$quiet" = "true" || echo "removing automake generated Makefile.in files"
  files=`find . -name 'Makefile.am' -print | sed -e 's%\.am%\.in%g'`
  for i in $files; do
    if test -f $i; then
      rm -f $i
      test "$verbose" = "-v" && echo "$i"
    fi
  done

  test "$quiet" = "true" || echo "removing configure files"
  files=`find . -name 'configure' -print`
  for i in $files; do
    if test -f $i; then
      rm -f $i
      test "$verbose" = "-v" && echo "$i"
    fi
  done

  if test $force -gt 0; then
    needles=""
    if test $force -gt 1; then
      # Manually maintained
      needles="$needles config.sub"
      needles="$needles config.guess"
    fi
    if test $force -gt 0; then
      # Inherited from automake
      needles="$needles compile"
      needles="$needles depcomp"
      needles="$needles install-sh"
      needles="$needles missing"
      needles="$needles mdate-sh"
    fi
    for j in $needles; do
      files=`find . -name "$j" -print`
      for i in $files; do
        if test -f $i; then
          rm -f $i
          test "$verbose" = "-v" && echo "$i"
        fi
      done
    done
  fi

  test "$quiet" = "true" || echo "removing aclocal.m4 files"
  files=`find . -name 'aclocal.m4' -print`
  test "$verbose" = "-v" && test -n "$files" && echo "$files"
  for i in $files; do
    if test -f $i; then
      rm -f $i
      test "$verbose" = "-v" && echo "$i"
    fi
  done

  find . -name '*~' -print | xargs rm -f
  find . -name 'bspopts.h.in' -print | xargs rm -f
  find . -name '*.orig' -print | xargs rm -f
  find . -name '*.rej' -print | xargs rm -f
  find . -name 'config.status' -print | xargs rm -f
  find . -name 'config.log' -print | xargs rm -f
  find . -name 'config.cache' -print | xargs rm -f
  find . -name 'Makefile' -print | xargs rm -f
  find . -name '.deps' -print | xargs rm -rf
  find . -name '.libs' -print | xargs rm -rf
  find . -name 'stamp-h.in' | xargs rm -rf
  find . -name 'autom4te*.cache' | xargs rm -rf
  ;;
esac

exit 0
